<script>
	//左侧用户组树选中标记
	var usergroupId = null;
	/**
	 * 点击用户组树
	 */
	function clickUsergroup(groupId) {
		printLog(groupId);
		if(groupId) {
			usergroupId = groupId;
			searchUser(groupId);
		}
	}
	
	var userSearchName = null;
	var userSearchValue = null;
	/**
	 * 禁用用户
	 */
	function disableUser(userId) {
		 var rows = user_userlist.datagrid('getChecked');
		    if (rows&&rows.length>0){
		        $.messager.confirm('询问','你确认要禁用用户吗?',function(r){
		        	if (r){
		            	var ids = "";
		            	for(var i=0; i<rows.length; i++){
		            		ids += rows[i].id+",";
		            	}
		                $.post('user.do?disableUserItems',{ids:ids},function(result){
		                    if (result.success){
		                    	reloadUser();    
		                    }
		                    showMsg(result.msg);
		                },'json');
		            }
		        });
		    }else {
		    	showMsg("请选择操作记录.");
		    }
	}
	/**
	 * 启用用户
	 */
	function enableUser() {
		var rows = user_userlist.datagrid('getChecked');
	    if (rows&&rows.length>0){
	        $.messager.confirm('询问','你确认要启用用户吗?',function(r){
	        	if (r){
	            	var ids = "";
	            	for(var i=0; i<rows.length; i++){
	            		ids += rows[i].id+",";
	            	}
	                $.post('user.do?enableUserItems',{ids:ids},function(result){
	                    if (result.success){
	                    	reloadUser();    
	                    }
	                    showMsg(result.msg);
	                },'json');
	            }
	        });
	    }else {
	    	showMsg("请选择操作记录.");
	    }
	}
	/**
	 * 用户角色分配
	 */
	function userRoleInfo(userId) {
		parent.$.modalDialog({
			title : '用户角色信息',
			width : 850,
			height : 540,
			href : 'user.do?userLinkRoles&userId='+userId,
			buttons : [ {
				text : '确定',
				handler : function() {
					parent.$.modalDialog.openner_userList = $('#user_userlist');
					parent.$.modalDialog.handler.action();
				}
			},{
				text : '取消',
				handler : function() {
					$.modalDialog.handler.dialog('destroy');
					$.modalDialog.handler = undefined;			}
			} ]
		});
	}
	/**
	 * 编辑用户信息
	 */
	function editUserInfo(userId) {
		printLog('user.do?editUser&userId='+userId);
		createFromWindow("新增用户信息", 'user.do?editUser&userId='+userId, "user.do?editUser",
				function(data) {
					reloadNew();
				});
	}
	
	
	//表格查询  
	function searchUser(){  
	    var params = {time : new Date().getTime()}; //先取得 datagrid 的查询参数  
		if (isNotEmpty(usergroupId)) {
			params['usergroupId'] = usergroupId;
		}
	    if (isNotEmpty(userSearchValue)) {
	    	params[userSearchName] = userSearchValue;
	    }
	    
	    user_userlist.datagrid({
			queryParams: params
		});
		printLog(params);
		reloadUser(); //设置好查询参数 reload 一下就可以了  
	}  
	//刷新用户组
	function userRefreshGroup() {
		//重置用户，默认用户组选择学达平台
		usergroupId = null;
		$('#groupTree').tree('reload'); 
		$('#user_userlist').datagrid({
			queryParams: {
				time : new Date().getTime()
			}
		});
		clearSearch();
	}
	
	
	function doSearchUser(value,name){
		userSearchName = name;
		userSearchValue = value;
		searchUser();
    }
	
	function clearSearch() {
		userSearchName = '';
		userSearchValue = '';
		$('#userSearchBox').searchbox('setValue','');
		user_userlist.datagrid({
			queryParams: {
				time : new Date().getTime()
			}
		});
		searchUser();
	}
	

	function formatterGroupTree(node) {
		var s = node.text;
		if (node.children.length > 0) {
			s += '&nbsp;<span style=\'color:blue;\'>' + node.children.length
					+ '</span>';
		}
		return s;
	}
	
	//工具按钮删除选中人员，勾选的那些记录
	
	/////////////////////////////////// js重构
	/**
	 * 工具按钮新增用户
	 */
	function addUser(){
		createFromWindow("新增用户信息", "user.do?editUser", "user.do?saveUser",
			function(data) {
				reloadUser();
			});
	}
	/**
	 * 工具按钮删除用户
	 */
	function deleteUser(){
	    var rows = user_userlist.datagrid('getChecked');
	    if (rows&&rows.length>0){
	        $.messager.confirm('询问','你确认要删除用户吗?',function(r){
	        	if (r){
	            	var ids = "";
	            	for(var i=0; i<rows.length; i++){
	            		ids += rows[i].id+",";
	            	}
	                $.post('user.do?deleteUserItems',{ids:ids},function(result){
	                    if (result.success){
	                    	reloadUser();    
	                    }
	                    showMsg(result.msg);
	                },'json');
	            }
	        });
	    }else {
	    	showMsg("请选择操作记录.");
	    }
	}
	/**
	 * 工具按钮编辑用户
	 */
	function editUser(){
	    var rows = user_userlist.datagrid('getChecked');
		if (rows&&rows.length>0) {
			if(rows.length>1) {
				showMsg('请选择一条操作记录.');
				return;
			}
			createFromWindow("编辑用户信息", "user.do?editUser&userId=" + rows[0].id,
					"user.do?saveUser", function(data) {
						reloadUser();
					},true);
		} else {
			showMsg('请选择操作记录.');
		}
	}
	/**
	 *用户表格重新加载
	 */
	function reloadUser() {
		var params = user_userlist.datagrid('options').queryParams; //先取得 datagrid 的查询参数  
		user_userlist.datagrid('reload'); //设置好查询参数 reload 一下就可以了  
		user_userlist.datagrid('unselectAll');
		user_userlist.datagrid('uncheckAll');
	}
	
	//记录后面操作按钮
	
	/**
	 * 禁用用户
	 */
	function operationsDisableUser(id) {
		if (id == undefined) {//点击右键菜单才会触发这个
			var rows = user_userlist.datagrid('getSelections');
			id = rows[0].id;
		}
		if (id) {
			 $.messager.confirm('询问','你确认要禁用用户吗?',function(r){
		        	if (r){
		                $.post('user.do?disableUserItems',{ids:id},function(result){
		                    if (result.success){
		                    	reloadUser();    
		                    }
		                    showMsg(result.msg);
		                },'json');
		            }
		        });
		} else {
			showMsg('请选择操作记录.');
		}
	}
	
	//操作按钮触发
	function operationsLookUser(id) {
		if (id == undefined) {//点击右键菜单才会触发这个
			var rows = user_userlist.datagrid('getSelections');
			id = rows[0].id;
		}
		if (id) {
			createFromWindow("查看用户信息", "user.do?editUser&userId=" + id,
					null, null,false);
		} else {
			showMsg('请选择查看记录.');
		}
	}
	
	function operationsEditUser(id) {
		if (id == undefined) {//点击右键菜单才会触发这个
			var rows = user_userlist.datagrid('getSelections');
			id = rows[0].id;
		}
		if (id) {
			createFromWindow("编辑用户信息", "user.do?editUser&userId=" +id,
					"user.do?saveUser", function(data) {
						reloadUser();
					},true);
		} else {
			showMsg('请选择编辑记录.');
		}
	}
	
	function operationsUserLinkRole(id) {
		if (id == undefined) {//点击右键菜单才会触发这个
			var rows = user_userlist.datagrid('getSelections');
			id = rows[0].id;
		}
		if (id) {
			createFromWindowOptions({
				title:"用户角色信息",
				maximizable:false,
				href:'user.do?userLinkRoles&userId='+id,
				callback:function(data) {
					saveSelectRoles();
				},
				enableSaveButton:true
			});
		} else {
			showMsg('请选择操作记录.');
		}
	}
	
</script>

<div class="easyui-layout" data-options="fit:true">
	<div data-options="region:'west',split:true,title:'用户组',collapsible:false,border:false,tools:[
					{
						iconCls:'icon-reload',
						handler:function(){userRefreshGroup()}
					}]" style="width:200px;">

		<@eu.tree id="groupTree" method="post" 
			url="group.do?usergroupTree" formatter="return formatterGroupTree(node)"
			onClick="clickUsergroup(node.id)"/>
	</div>
	<div data-options="region:'center',border:false">
		<#assign operations = "[\{shiro:'',onClick:'operationsLookUser',src:'static/jmax/images/notes/note.png',title:'查看'},
	   					\{shiro:'sys:user:edit',onClick:'operationsEditUser',src:'static/jmax/images/notes/note_edit.png',title:'编辑'},
	   					\{shiro:'sys:user:linkRole',onClick:'operationsUserLinkRole',src:'static/jmax/images/notes/note_go.png',title:'角色信息'},
	   					\{shiro:'sys:user:disable',onClick:'operationsDisableUser',src:'static/jmax/images/notes/note_delete.png',title:'禁用'}
	  				   ]"/>
	  				   
		<@eu.datagrid id="user_userlist" idField="id"
		toolbar="#userlist_search"  operations="${operations!}"
		url="user.do?loadByCondition" item="com.xdtech.sys.vo.UserItem" />
		<div id="userlist_search"> 
			<div style="margin-bottom:5px;padding-left:5px;margin-top: 5px;">
				<input id="userSearchBox"  style="width:300px"></input>
				<div id="userSearchMenu" style="width:150px">
				    <div data-options="name:'loginName'">登录名</div>
				    <div data-options="name:'name'">姓名</div>
				</div>
				<script type="text/javascript">
					$('#userSearchBox').searchbox({
					    searcher:function(value,name){
					    	doSearchUser(value,name)
					    },
					    menu:'#userSearchMenu',
					    prompt:'请输入查询内容'
					});   
				</script>
				<@eu.linkbutton showName="清空" shiro="sys:user:clear" iconCls="icon-clear" onclick="clearSearch()"/>
			</div>
			<div id="user_toolbar" style="height: auto">
				<@eu.linkbutton showName="新增" shiro="sys:user:add" iconCls="icon-add" onclick="addUser()"/>
				<@eu.linkbutton showName="编辑" shiro="sys:user:edit" iconCls="icon-edit" onclick="editUser()"/>
				<@eu.linkbutton showName="删除" shiro="sys:user:delete" iconCls="icon-remove" onclick="deleteUser()"/>
				<@eu.linkbutton showName="禁用" shiro="sys:user:disable" iconCls="icon-disable" onclick="disableUser()"/>
				<@eu.linkbutton showName="启用" shiro="sys:user:enable" iconCls="icon-ok" onclick="enableUser()"/>
				<@eu.linkbutton showName="刷新" shiro="sys:user:refresh" iconCls="icon-reload" onclick="reloadUser()"/>
			</div>
		</div>
		
	</div>
</div>
